package com.wangyadong.hobby.javanetwork.socket;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Comparator;
import java.util.stream.IntStream;

public class MySocketTest {

    public static void main(String[] args) {

        avalibablePort();
//        Socket socket = null;
//        try {
//            socket = new Socket("www.dict.org", 2628);
//            socket.setSoTimeout(3000);
//            try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
//                System.out.println(reader.readLine());
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    public static void avalibablePort() {
        IntStream.range(0, 10).forEach(i -> {
            try {
                InetAddress localHost = InetAddress.getLocalHost();
                Socket socket = new Socket(localHost.getHostAddress(), i);
                System.out.println(i);
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
                System.err.printf("-------------->" + i);
            }
        });
    }
}
